package com.buka.recipe.common.utils;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;

/**
 * @author admin
 */
public class AddressUtils {

    public static final String IP_URL = "http://whois.pconline.com.cn/ipJson.jsp";

    public static final String UNKNOWN = "未知";

    public static String getRealAddressByIP(String ip) {
        try {
            String rspStr = HttpUtil.get(IP_URL + "?ip=" + ip + "&json=true", 1000);
            if (StrUtil.isEmpty(rspStr)) {
                return UNKNOWN;
            }
            JSONObject obj = JSON.parseObject(rspStr);
            String addr = obj.getString("addr");
            return StrUtil.isNotBlank(addr) ? addr : UNKNOWN;
        } catch (Exception e) {
            return UNKNOWN;
        }
    }
}
